package com.ruoyi.officeInfo.service.impl;

import java.util.List;

import cn.hutool.core.bean.BeanUtil;
import com.ruoyi.basic.domain.AtbShap;
import com.ruoyi.basic.mapper.AtbShapMapper;
import com.ruoyi.common.filter.ID;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.objectPic.domain.AtbObjectPic;
import com.ruoyi.objectPic.mapper.AtbObjectPicMapper;
import com.ruoyi.officeInfo.domain.AtbOfficeInfoDto;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.officeInfo.mapper.AtbOfficeInfoMapper;
import com.ruoyi.officeInfo.domain.AtbOfficeInfo;
import com.ruoyi.officeInfo.service.IAtbOfficeInfoService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 写字楼详细信息Service业务层处理
 * 
 * @author ruoyi
 * @date 2024-09-16
 */
@Service
public class AtbOfficeInfoServiceImpl implements IAtbOfficeInfoService 
{
    @Autowired
    private AtbOfficeInfoMapper atbOfficeInfoMapper;


    @Autowired
    private AtbShapMapper atbShapMapper;


    @Autowired
    private AtbObjectPicMapper atbObjectPicMapper;

    /**
     * 查询写字楼详细信息
     * 
     * @param id 写字楼详细信息主键
     * @return 写字楼详细信息
     */
    @Override
    public AtbOfficeInfo selectAtbOfficeInfoById(Long id)
    {
        return atbOfficeInfoMapper.selectAtbOfficeInfoById(id);
    }

    /**
     * 查询写字楼详细信息列表
     * 
     * @param atbOfficeInfo 写字楼详细信息
     * @return 写字楼详细信息
     */
    @Override
    public List<AtbOfficeInfo> selectAtbOfficeInfoList(AtbOfficeInfo atbOfficeInfo)
    {
        return atbOfficeInfoMapper.selectAtbOfficeInfoList(atbOfficeInfo);
    }




    /**
     * 新增写字楼详细信息
     * 
     * @param atbOfficeInfoDto 写字楼详细信息
     * @return 结果
     */
    @Override
    @Transactional
    public int insertAtbOfficeInfo(AtbOfficeInfoDto atbOfficeInfoDto)
    {


//        ID id = new ID();
//        atbOfficeInfo.setId(id.getId());
//        atbOfficeInfo.setCreateTime(DateUtils.getNowDate());
        ID id = new ID();
        long id1 = id.getId();
        AtbOfficeInfo atbOfficeInfo = BeanUtil.toBean(atbOfficeInfoDto, AtbOfficeInfo.class);
        atbOfficeInfo.setId(id1);
        atbOfficeInfo.setOfficeBuildingInfo(atbOfficeInfoDto.getOfficeBuildingInfo());
        atbOfficeInfo.setDistrictId(atbOfficeInfoDto.getDistrictId());
        atbOfficeInfo.setShopTypeId(atbOfficeInfoDto.getShopTypeId());
        atbOfficeInfo.setGrade(atbOfficeInfoDto.getGrade());
        atbOfficeInfo.setFloor(atbOfficeInfoDto.getFloor());
        atbOfficeInfo.setRegister(atbOfficeInfoDto.getRegister());
        atbOfficeInfo.setFitment(atbOfficeInfoDto.getFitment());
        atbOfficeInfo.setShopPay(atbOfficeInfoDto.getShopPay());
        atbOfficeInfo.setTenancyTerm(atbOfficeInfoDto.getTenancyTerm());
        atbOfficeInfo.setIsExamine(atbOfficeInfoDto.getIsExamine());
        atbOfficeInfo.setIsPutaway(atbOfficeInfoDto.getIsPutaway());
        atbOfficeInfo.setCreateTime(DateUtils.getNowDate());
        atbOfficeInfo.setUpdateTime(DateUtils.getNowDate());



        AtbShap atbShap = new AtbShap();
        atbShap.setId(id1);
        atbShap.setShopMessage(atbOfficeInfoDto.getShopMessage());
        atbShap.setShopArea(atbOfficeInfoDto.getShopArea());
        atbShap.setShopDistance(atbOfficeInfoDto.getShopDistance());
        atbShap.setShopStatus(atbOfficeInfoDto.getShopStatus());
        atbShap.setPrice(atbOfficeInfoDto.getPrice());
        atbShap.setSourceId(atbOfficeInfoDto.getSourceId());
        atbShap.setSelectType(atbOfficeInfoDto.getSelectType());
        atbShap.setMovies(atbOfficeInfoDto.getMovies());
        atbShap.setCity1(atbOfficeInfoDto.getCity1());
        atbShap.setCity2(atbOfficeInfoDto.getCity2());
        atbShap.setCity3(atbOfficeInfoDto.getCity3());
        atbShap.setAddress(atbOfficeInfoDto.getAddress());
        atbShap.setAddressLongitude(atbOfficeInfoDto.getAddressLongitude());
        atbShap.setAddressLatitude(atbOfficeInfoDto.getAddressLatitude());
        atbShap.setAddress(atbOfficeInfoDto.getAddress());
        atbShap.setCreateTime(DateUtils.getNowDate());
        atbShap.setUpdateTime(DateUtils.getNowDate());
        atbShapMapper.insertAtbShap(atbShap);


        String[] split = atbShap.getMovies().split(",");
        for (String s : split) {

            //图片添加
            AtbObjectPic atbObjectPic = new AtbObjectPic();
            atbObjectPic.setUrl(s);
            atbObjectPic.setObjectId(atbShap.getId());
            atbObjectPicMapper.insertAtbObjectPic(atbObjectPic);
        }




        return atbOfficeInfoMapper.insertAtbOfficeInfo(atbOfficeInfo);
    }

    /**
     * 修改写字楼详细信息
     * 
     * @param atbOfficeInfo 写字楼详细信息
     * @return 结果
     */
    @Override
    public int updateAtbOfficeInfo(AtbOfficeInfo atbOfficeInfo)
    {

        return atbOfficeInfoMapper.updateAtbOfficeInfo(atbOfficeInfo);
    }

    /**
     * 批量删除写字楼详细信息
     * 
     * @param ids 需要删除的写字楼详细信息主键
     * @return 结果
     */
    @Override
    public int deleteAtbOfficeInfoByIds(Long[] ids)
    {
        for (Long id : ids) {
            List<AtbObjectPic> atbObjectPicList  = atbObjectPicMapper.selectList(id);

            for (AtbObjectPic atbObjectPic : atbObjectPicList) {
                atbObjectPicMapper.deleteAtbObjectPicById(atbObjectPic.getId());
            }

        }
        atbShapMapper.deleteAtbShapByIds(ids);
        return atbOfficeInfoMapper.deleteAtbOfficeInfoByIds(ids);
    }

    /**
     * 删除写字楼详细信息信息
     * 
     * @param id 写字楼详细信息主键
     * @return 结果
     */
    @Override
    public int deleteAtbOfficeInfoById(Long id)
    {
        atbShapMapper.deleteAtbShapById(id);
        //根据id查看图片图片集合

        List<AtbObjectPic> atbObjectPicList  = atbObjectPicMapper.selectList(id);

        for (AtbObjectPic atbObjectPic : atbObjectPicList) {
            atbObjectPicMapper.deleteAtbObjectPicById(atbObjectPic.getId());
        }
        return atbOfficeInfoMapper.deleteAtbOfficeInfoById(id);
    }
}
